package org.dashboard.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import org.dashboard.bean.AdAuthorization;
import org.dashboard.bean.AdPermission;
import org.dashboard.constant.Constant;
import org.dashboard.dao.AdAuthorizationMapper;
import org.dashboard.dao.AdPermissionMapper;
import org.dashboard.service.AdAuthorizationService;
import org.dashboard.service.AdPermissionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = AdPermissionService.class)
@CacheConfig(cacheManager = Constant.DEFAULT_CACHE_MANAGER)
public class AdPermissionServiceImpl implements AdPermissionService {
    private static final Logger logger = LoggerFactory.getLogger(AdPermissionServiceImpl.class);

    @Autowired
    private AdPermissionMapper adPermissionMapper;

    @Autowired
    private AdAuthorizationService adAuthorizationService;

    @Override
    public int deleteByPrimaryKey(Integer permissionId) {
        return adPermissionMapper.deleteByPrimaryKey(permissionId);
    }

    @Override
    public int insert(AdPermission record) {
        adPermissionMapper.insert(record);
        return record.getPermissionId();
    }

    @Override
    public int insertSelective(AdPermission record) {
        adPermissionMapper.insertSelective(record);
        return record.getPermissionId();
    }

    @Override
    public AdPermission selectByPrimaryKey(Integer permissionId) {
        return adPermissionMapper.selectByPrimaryKey(permissionId);
    }

    @Override
    public int updateByPrimaryKeySelective(AdPermission record) {
        return adPermissionMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(AdPermission record) {
        return adPermissionMapper.updateByPrimaryKey(record);
    }


    @Override
    @Transactional
    public void saveUserRoles(List<Integer> roleIds, Integer userId) {
        adPermissionMapper.deleteByUserId(userId);
        for (Integer roleId : roleIds) {
            AdPermission adPermission = new AdPermission();
            adPermission.setRoleId(roleId);
            adPermission.setUserId(userId);
            adPermissionMapper.insert(adPermission);
        }
    }

    @Override
    public void saveRoleResources(Integer roleId, Integer resourceType, List<Integer> resourceIdList) {
        adAuthorizationService.deleteByRoleIdResourceType(roleId, resourceType);
        for (Integer resourceId : resourceIdList) {
            AdAuthorization adAuthorization = new AdAuthorization();
            adAuthorization.setResourceId(resourceId);
            adAuthorization.setResourceType(resourceType);
            adAuthorization.setRoleId(roleId);
            adAuthorizationService.insert(adAuthorization);
        }
    }

}
